package com.aneliya.proxy;

import com.aneliya.command.loginDialog.LoginFailedCommand;
import com.aneliya.command.loginDialog.LoginSuccessfulCommand;
import com.aneliya.command.loginDialog.RegistrationFailedCommand;
import com.aneliya.framework.mvc.Facade;
import com.aneliya.framework.mvc.model.Proxy;
import com.aneliya.model.Message;
import com.aneliya.model.MessageType;
import com.aneliya.model.User;

public class UserProxy extends Proxy {
	public static final String LOGIN_SUCCESSFUL_EVENT = "loginSuccessful";
	public static final String LOGIN_FAILED_EVENT = "loginFailed";
	public static final String REGISTRATION_FAILED_EVENT = "registrationFailed";
	
	private User authenticatedUser;
	public User getAuthenticatedUser() {
		return authenticatedUser;
	}
	public void setAuthenticatedUser(User authenticatedUser) {
		this.authenticatedUser = authenticatedUser;
	}
	
	public UserProxy() throws Exception {
		name = "UserProxy";
		facade = Facade.getInstance();
		
		initializeCommands();
	}
	
	private void initializeCommands() throws Exception {
		facade.registerCommand(LOGIN_SUCCESSFUL_EVENT, LoginSuccessfulCommand.class);
		facade.registerCommand(LOGIN_FAILED_EVENT, LoginFailedCommand.class);
		facade.registerCommand(REGISTRATION_FAILED_EVENT, RegistrationFailedCommand.class);
	}

	public void login(User user) throws Exception {
		authenticatedUser = user;
		Message loginMessage = new Message(null, user);
		loginMessage.setMessageType(MessageType.LOGIN);
		
		ServerProxy serverProxy = (ServerProxy) facade.retrieveProxy("ServerProxy");
		serverProxy.sendMessage(loginMessage);
	}
	
	public void register(User user) throws Exception {
		authenticatedUser = user;
		Message registerMessage = new Message(null, user);
		registerMessage.setMessageType(MessageType.REGISTER);
		
		ServerProxy serverProxy = (ServerProxy) facade.retrieveProxy("ServerProxy");
		serverProxy.sendMessage(registerMessage);
	}

}
